using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DocLibApp
{
    public partial class FormLogin : FormBase
    {
        private string sAuthenUser = "";

        private bool IsAuthenticateSuccessful = false;
        private int iFailCount = 1;
        private SQLiteDB mySQLiteDB = null;

        public string AuthenticatedUser
        {
            get
            {
                return this.sAuthenUser;
            }

            set
            {
                this.sAuthenUser = value;
            }
        }

        public bool IsAuthenticated
        {
            get
            {
                return IsAuthenticateSuccessful;
            }
        }

        public FormLogin()
        {
            InitializeComponent();

            //remove after testing
            txtUsername.Text = def.PowerUserName;
            txtPassword.Text = def.PowerUserPassword;

            util.GetSettingHash(settingHash, def.FileName_DefaultConfig);

            SQLiteDatabaseFileName = GetSettingString(def.ck_DatabaseFileName_SQLite, SQLiteDatabaseFileName);

            iFailAttemptNo = GetSettingInt("FailAttemptNo", iFailAttemptNo);
            //string password = GetSettingString("DatabaseFilePassword", "");
            mySQLiteDB = new SQLiteDB(SQLiteDatabaseFileName);
            mySQLiteDB.OpenConnection();
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUsername.Text.Trim()) && !string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                string user = txtUsername.Text.Trim();
                string password = txtPassword.Text.Trim();

                if (user == def.PowerUserName && password == def.PowerUserPassword)
                {
                    sAuthenUser = def.PowerUserName;

                    IsAuthenticateSuccessful = true;
                    OpenMainForm();
                }
                else
                {
                    System.Collections.Hashtable ht = new System.Collections.Hashtable();
                    ht["USERNAME"] = user;
                    ht["PASSWORD"] = util.MaskString(password);

                    if (doc_security.AuthenticateUser(ht, mySQLiteDB))
                    {
                        sAuthenUser = user;
                        this.IsAuthenticateSuccessful = true;
                        OpenMainForm();
                    }
                    else if (iFailCount < iFailAttemptNo)
                    {
                        iFailCount++;
                        ShowMessage("You have entered a wrong username / password combination.");
                        this.IsAuthenticateSuccessful = false;
                    }
                    else
                    {
                        ShowMessage("You have attempt to login with wrong credential for " + iFailAttemptNo.ToString() + " times, will exit.");
                        this.IsAuthenticateSuccessful = false;
                        this.Close();
                    }
                }
            }
            else
            {
                ShowMessage("Please enter both a user name and password to login to the system.");
                this.IsAuthenticateSuccessful = false;
            }

        }

        private void OpenMainForm()
        {
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (mySQLiteDB != null)
                mySQLiteDB.CloseConnection();

            //ShowMessage("No user name and password entered, quitting the program");

            this.Close();
        }
    }
}